package cn.itcast.service.impl;

import cn.itcast.dao.RoleDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.pojo.Role;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //开启分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //根据分页查询信息
        List<Role> roles = roleDao.selectByCondition(queryPageBean.getQueryString());
        //封装结果 并返回
        PageResult pageResult = new PageResult(new Long(roles.size()), roles);

        return pageResult;
    }


    //查询所有角色
    @Override
    public List<Role> findAllRole() {

        List<Role> roles = roleDao.selectByCondition(null);

        return roles;
    }

    //根据id查询role
    @Override
    public Role findRoleById(Integer id) {
        Role role = roleDao.findRoleById(id);
        return role;
    }

    //添加角色
    @Override
    @Transactional
    public void addRole(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //先向Role表中插入role信息
        this.roleDao.add(role);

        //将roleId 和 permissionIds插入中间表
        for (Integer permissionId : permissionIds) {
            this.roleDao.insertRoleAndPermissionId(role.getId(), permissionId);
        }

        //将roleId 和 menuIds插入中间表
        for (Integer menuId : menuIds) {
            this.roleDao.insertRoleAndMenuId(role.getId(), menuId);
        }
    }


    //删除用户
    @Override
    @Transactional
    public void deleteRoleById(Integer roleId) {
        //先删除中间表
        this.roleDao.deleteRoleAndPermissionId(roleId);
        this.roleDao.deleteRoleAndMenuId(roleId);

        //删除role
        this.roleDao.deleteRoleById(roleId);
    }

    @Override
    public ArrayList<Integer> findMenuIds(Integer roleId) {
        ArrayList<Integer> menuIds = this.roleDao.findMenuIds(roleId);

        return menuIds;
    }

    @Override
    public ArrayList<Integer> findPermissionIds(Integer roleId) {
        ArrayList<Integer> permissionIds = this.roleDao.findPermissionIds(roleId);
        return permissionIds;
    }


    //修改
    @Override
    @Transactional
    public void editRole(Role role, Integer[] permissionIds, Integer[] menuIds) {
        //向t_role添加role数据
        this.roleDao.updateRole(role);

        //将roleId 和 permissionIds插入中间表
        this.roleDao.deleteRoleAndPermissionId(role.getId());
        for (Integer permissionId : permissionIds) {
            this.roleDao.insertRoleAndPermissionId(role.getId(), permissionId);
        }

        //将roleId 和 menuIds插入中间表
        this.roleDao.deleteRoleAndMenuId(role.getId());
        for (Integer menuId : menuIds) {
            this.roleDao.insertRoleAndMenuId(role.getId(), menuId);
        }
    }
}
